package org.kordakus.shoot.model.util;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public final class ExifFormatUtil
{

	private static final Logger	_logger	= Logger.getLogger(ExifFormatUtil.class);

	private ExifFormatUtil()
	{
	}

	public static DateTime parseExifDate(String parse)
	{
		_logger.debug("Parsing string data to date (input=" + parse + ")");
		try
		{
			String[] fragments = parse.split(" ");
			String date = fragments[0];
			String time = fragments[1];

			String[] dateFragments = date.split(":");
			String year = dateFragments[0];
			String month = dateFragments[1];
			String day = dateFragments[2];

			String[] timeFragments = time.split(":");
			String hour = timeFragments[0];
			String minute = timeFragments[1];
			String second = timeFragments[2];

			DateTime result = new DateTime(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day),
					Integer.valueOf(hour), Integer.valueOf(minute), Integer.valueOf(second), DateTimeZone.getDefault());
			_logger.debug("Parsing string data to date (result=" + result + ")");
			return result;
		}
		catch (Exception e)
		{
			_logger.error("Unable to parse string parameter (" + e.getMessage() + ")");
			_logger.debug("Unable to parse string parameter", e);
		}

		return null;
	}

	public static String formatExifDate(DateTime format)
	{
		String result = format.getYear() + ":" + format.getMonthOfYear() + ":" + format.getDayOfMonth() + " "
				+ format.getHourOfDay() + ":" + format.getMinuteOfHour() + ":" + format.getSecondOfMinute();
		_logger.debug("Formatted datetime parameter into " + result);
		return result;
	}

}
